﻿using Assets.Core.Common.Web;
using Assets.Core.Jwt;
using Masuit.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;
using Nucleic.Web.Dto.SampleResult;
using Nucleic.Web.Models.SampleResult;

namespace Nucleic.Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SampleResultController : ControllerBase
    {
        private readonly ILogger<SampleResultController> _logger;
        public UserInfo? UserInfo
        {
            get
            {
                var token = HttpContext.Request.Headers["token"].ToString();
                if (string.IsNullOrEmpty(token))
                {
                    return null;
                }
                var result = JwtHelp.CheckToken(token);
                if (result.Success)
                {
                    return result.Data;
                }
                else
                {
                    return null;
                }
            }
        }

        public SampleResultController(ILogger<SampleResultController> logger)
        {
            _logger = logger;

        }
        [HttpPost]
        [Route("GetByOrderId2")]
        public Result GetByOrderId2(string orders_id)
        {
            //var sample_id = SqlSugarFactory.Instance.Queryable<v_report_patient>()
            //   .First();
            ////var items = SqlSugarFactory.Instance.SqlQueryable<v_report_patient>($"select * from v_report_patient where sample_id='{sample_id}'")
            ////     .ToList();
            //return Result.success(sample_id);
            var items = SqlSugarFactory.Instance.Queryable<v_report_patient>()
                .InnerJoin<sample_patient>((t1, t2) => t1.sample_id == t2.sample_id)
                .InnerJoin<book_info>((t1, t2, t3) => t3.appo_id == t2.appo_id)
                .Where((t1, t2, t3) => t3.orders_id == orders_id).Select((t1, t2, t3) => t1).ToList();
            //var items = SqlSugarFactory.Instance.SqlQueryable<v_report_patient>($"select * from v_report_patient where sample_id='{sample_id}'")
            //     .ToList();
            return Result.success(items);
        }
        /// <summary>
        /// 根据订单号查询核酸结果
        /// </summary>
        /// <param name="orders_id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetByOrderId")]
        public Result GetByOrderId(string orders_id)
        {
            var sample_id = SqlSugarFactory.Instance.Queryable<sample_patient>()
                .InnerJoin<book_info>((t1, t2) => t1.appo_id == t2.appo_id)
                .Where((t1, t2) => t2.orders_id == orders_id).Select((t1, t2) => t1.sample_id).First();
            var items = SqlSugarFactory.Instance.SqlQueryable<v_report_patient>($"select * from v_report_patient where sample_id='{sample_id}'")
                 .ToList();
            return Result.success(items);
        }
        /// <summary>
        /// 获取单个结果
        /// </summary>
        /// <param name="appo_id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Get")]
        public Result<SampleResultQueryDto> Get(int appo_id)
        {
            var sample_resu = SqlSugarFactory.Instance.Queryable<sample_patient>()
                 .InnerJoin<sample_info>((t1, t2) => t1.sample_id == t2.sample_id)
                  .InnerJoin<sample_result>((t1, t2, t3) => t1.sample_id == t3.sample_id)
                 .Where((t1, t2, t3) => t1.appo_id == appo_id)
                 .Select((t1, t2, t3) => t3).First();
            if (sample_resu == null)
            {
                return Result<SampleResultQueryDto>.fail("未找到");
            }
            var items = SqlSugarFactory.Instance.Queryable<sample_result>()
          .InnerJoin<patient_info>((t1, t2) => t1.pat_id == t2.pat_id)
          .InnerJoin<check_type>((t1, t2, t3) => t1.type_id == t3.type_id)
          .InnerJoin<check_item>((t1, t2, t3, t4) => t3.item_id == t4.item_id)
          .Where((t1, t2, t3, t4) => t1.sample_id == sample_resu.sample_id)
          .OrderBy((t1, t2, t3, t4) => t1.report_time, SqlSugar.OrderByType.Desc)
          .Select((t1, t2, t3, t4) => new SampleResultQueryDto
          {
              pat_name = t1.pat_name,
              sex = t1.sex,
              idnum = t1.idnum,
              addr = t1.addr,
              tel = t1.tel,
              result = t1.result,
              collect_time = t1.collect_time,
              intime = t1.intime,
              report_time = t1.report_time,
              type_name = t3.name,
              item_name = t4.name
          }).First();
            return Result<SampleResultQueryDto>.success(items);
        }

        /// <summary>
        /// 查询检测结果
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Query")]
        public Result<PagedResultDto<SampleResultQueryDto>> Query(SampleResultQueryInput input)
        {
            
            var totalCount = 0;
            var items = SqlSugarFactory.Instance.Queryable<sample_result>()
                .InnerJoin<patient_info>((t1, t2) => t1.pat_id == t2.pat_id)
                .InnerJoin<check_type>((t1, t2, t3) => t1.type_id == t3.type_id)
                .InnerJoin<check_item>((t1, t2, t3, t4) => t3.item_id == t4.item_id)
                .InnerJoin<sample_patient>((t1, t2, t3, t4, t5) => t1.sample_id == t5.sample_id)
                .InnerJoin<book_info>((t1, t2, t3, t4, t5, t6) => t5.appo_id == t6.appo_id)

                .Where((t1, t2, t3, t4, t5, t6) => t1.idnum == input.idnum)
                .OrderBy((t1, t2, t3, t4, t5, t6) => t1.report_time, SqlSugar.OrderByType.Desc)
                .Select((t1, t2, t3, t4, t5, t6) => new SampleResultQueryDto
                {
                    pat_name = t1.pat_name,
                    sex = t1.sex,
                    idnum = t1.idnum,
                    addr = t1.addr,
                    tel = t1.tel,
                    result = t1.result,
                    collect_time = t1.collect_time,
                    intime = t1.intime,
                    report_time = t1.report_time,
                    type_name = t3.name,
                    item_name = t4.name,
                    orders_id = t6.orders_id
                }).ToPageList(input.pageIndex, input.pageSize, ref totalCount);
            var result = new PagedResultDto<SampleResultQueryDto>(
              totalCount, items.Select(item =>
              {
                  var dto = new SampleResultQueryDto();
                  dto.pat_name = item.pat_name;
                  dto.sex = item.sex;
                  dto.idnum = item.idnum;
                  dto.addr = item.addr;
                  dto.tel = item.tel;
                  dto.result = item.result;
                  dto.collect_time = item.collect_time;
                  dto.intime = item.intime;
                  dto.report_time = item.report_time;
                  dto.type_name = item.type_name;
                  dto.item_name = item.item_name;
                  dto.orders_id = item.orders_id;
                  return dto;
              }).ToList());
            return Result<PagedResultDto<SampleResultQueryDto>>.success(result);
        }
    }
}
